package cn.zzp.org.service.impl;

import cn.zzp.basic.service.impl.BaseServiceImpl;
import cn.zzp.basic.util.Md5Utils;
import cn.zzp.basic.util.StrUtils;
import cn.zzp.org.domain.Employee;
import cn.zzp.org.mapper.EmployeeMapper;
import cn.zzp.org.service.EmployeeService;
import cn.zzp.user.domain.LoginInfo;
import cn.zzp.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName EmployeeServiceImpl
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/14 14:16
 */
@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    /**
     * 添加
     * @param employee
     */
    @Transactional
    @Override
    public void add(Employee employee) {
        LoginInfo loginInfo = getLoginInfo(employee);

        loginInfoMapper.add(loginInfo);

        employee.setLogininfo_id(loginInfo.getId());
        employeeMapper.add(employee);

        // 添加中间表信息
        employeeMapper.addEmployeeRole(employee);
    }

    public LoginInfo employee2LoginInfo(Employee employee) {
        LoginInfo loginInfo = new LoginInfo();

        BeanUtils.copyProperties(employee, loginInfo);

        // 设置管理员权限
        loginInfo.setType(0);

        return loginInfo;
    }

    /**
     * 更新操作
     * @param employee
     */
    @Transactional
    @Override
    public void update(Employee employee){
        LoginInfo loginInfo = getLoginInfo(employee);

        Long logininfo_id = employeeMapper.queryById(employee.getId()).getLogininfo_id();
        loginInfo.setId(logininfo_id);

        employeeMapper.update(employee);
        loginInfoMapper.update(loginInfo);

        employeeMapper.deleteEmployeeRoleByRoleId(employee.getId());
        employeeMapper.addEmployeeRole(employee);

    }

    /**
     * 盐值密码和同步loginInfo
     * @param employee
     * @return
     */
    private LoginInfo getLoginInfo(Employee employee) {
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);

        // 获取加密密码
        String password = Md5Utils.encrypByMd5(salt + employee.getPassword());

        employee.setSalt(salt);
        employee.setPassword(password);
        // 同步LoginInfo
        return employee2LoginInfo(employee);
    }

    /**
     * 删除
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id){
        // 删除员工的时候需要将登录信息删除
        Long logininfo_id = employeeMapper.queryById(id).getLogininfo_id();
        loginInfoMapper.delete(logininfo_id);

        employeeMapper.deleteEmployeeRoleByRoleId(id);

        employeeMapper.delete(id);
    }

    @Override
    public List<Employee> list(Long shopId) {

        return employeeMapper.list(shopId);

    }
}
